package com.ruoyi.playlist.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.playlist.domain.Playlist;
import com.ruoyi.playlist.service.IPlaylistService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 歌单Controller
 * 
 * @author lin
 * @date 2025-03-04
 */
@RestController
@RequestMapping("/playlist/playlist")
public class PlaylistController extends BaseController
{
    @Autowired
    private IPlaylistService playlistService;

    /**
     * 查询歌单列表
     */
    @PreAuthorize("@ss.hasPermi('playlist:playlist:list')")
    @GetMapping("/list")
    public TableDataInfo list(Playlist playlist)
    {
        startPage();
        List<Playlist> list = playlistService.selectPlaylistList(playlist);
        return getDataTable(list);
    }

    /**
     * 导出歌单列表
     */
    @PreAuthorize("@ss.hasPermi('playlist:playlist:export')")
    @Log(title = "歌单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Playlist playlist)
    {
        List<Playlist> list = playlistService.selectPlaylistList(playlist);
        ExcelUtil<Playlist> util = new ExcelUtil<Playlist>(Playlist.class);
        util.exportExcel(response, list, "歌单数据");
    }

    /**
     * 获取歌单详细信息
     */
    @PreAuthorize("@ss.hasPermi('playlist:playlist:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(playlistService.selectPlaylistById(id));
    }

    /**
     * 新增歌单
     */
    @PreAuthorize("@ss.hasPermi('playlist:playlist:add')")
    @Log(title = "歌单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Playlist playlist)
    {
        return toAjax(playlistService.insertPlaylist(playlist));
    }

    /**
     * 修改歌单
     */
    @PreAuthorize("@ss.hasPermi('playlist:playlist:edit')")
    @Log(title = "歌单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Playlist playlist)
    {
        return toAjax(playlistService.updatePlaylist(playlist));
    }

    /**
     * 删除歌单
     */
    @PreAuthorize("@ss.hasPermi('playlist:playlist:remove')")
    @Log(title = "歌单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(playlistService.deletePlaylistByIds(ids));
    }

//    @PreAuthorize("@ss.hasPermi('playlist:playlist:list')")
    @GetMapping("/getTopPlaylistByUserID/{userId}")
    public TableDataInfo listByUserId(@PathVariable("userId") Integer userId) {
        startPage();
        List<Playlist> list = playlistService.selectPlaylistByUserId(userId);
        return getDataTable(list);
    }
}
